package com.kyzh.core.adapters

import android.content.Intent
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.RatingBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gushenge.atools.ui.ArcButton
import com.gushenge.core.beans.Game
import com.kyzh.core.R
import com.kyzh.core.activities.H5Activity
import com.kyzh.core.pager.login.LoginActivity
import com.kyzh.core.utils.*
import org.jetbrains.anko.backgroundResource

class RankAdapter(layout: Int, beans: MutableList<Game>) : BaseQuickAdapter<Game, BaseViewHolder>(layout, beans) {
    override fun convert(helper: BaseViewHolder, item: Game) {
        item.let {
            val root = helper.getView<ConstraintLayout>(R.id.h6)
            val bq = helper.getView<RecyclerView>(R.id.bq)
            val desc = helper.getView<TextView>(R.id.desc)
            val start = helper.getView<Button>(R.id.start)
            val cup = helper.getView<ImageView>(R.id.rankCup)
            val num = helper.getView<TextView>(R.id.rankNum)
            val discount = helper.getView<ArcButton>(R.id.btDiscount)

            val rbStar = helper.getView<RatingBar>(R.id.rbStar)

            if(item.tuijianzhishu.isNullOrEmpty()){
                rbStar.rating = item.point.toFloat()
            }else{
                rbStar.rating = item.tuijianzhishu.toFloat()
            }

            when (helper.adapterPosition) {
                0 -> {
                    num.visibility = View.GONE
                    cup.visibility = View.VISIBLE
                    cup.backgroundResource = R.drawable.rank_first
                }

                1 -> {
                    num.visibility = View.GONE
                    cup.visibility = View.VISIBLE
                    cup.backgroundResource = R.drawable.rank_second
                }

                2 -> {
                    num.visibility = View.GONE
                    cup.visibility = View.VISIBLE
                    cup.backgroundResource = R.drawable.rank_third
                }

                else -> {
                    num.visibility = View.VISIBLE
                    cup.visibility = View.GONE
                    num.text = "${helper.adapterPosition + 1}"
                }
            }
            helper.setText(R.id.name, it.name)
                .setText(R.id.desc, it.summary)
                .setBackgroundResource(R.id.start, R.drawable.button_bg)
            Glide.with(context).load(it.icon).into(helper.getView(R.id.image))
            root.setOnClickListener { context.startGameDetailActivity(item.gid.toString()) }

            if(item.zhekou.isNullOrEmpty()){
                discount.setVisibility(false)
            }else{
                if(!item.zhekou.contains("折")){
                    discount.text = item.zhekou+"折"
                }else{
                    discount.text = item.zhekou
                }
                discount.setVisibility(true)
                if(item.zhekou.contains("10")){
                    discount.setVisibility(false)
                }
            }

            if (item.biaoqian.isNotEmpty()) {
                bq.visibility = View.VISIBLE
                desc.visibility = View.GONE

                bq.layoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
                bq.adapter = BiaoqianAdapter( item.biaoqian)
            } else {
                bq.visibility = View.GONE
                desc.visibility = View.VISIBLE
            }

            when (it.system_type) {
                1 -> {
                    helper.setText(R.id.type, "${item.size}M | ${item.type}")
                        .setText(R.id.start, context.getString(R.string.show))
                    start.setOnClickListener {
                        if (isLogin()) {
                            context.startGameDetailActivity(item.gid.toString())
                        } else {
                            context.startActivity<LoginActivity>()
                        }
                    }
                }

                2 -> {
                    helper.setText(R.id.type, "${item.type}")
                        .setText(R.id.start, context.getString(R.string.open))
                    start.setOnClickListener {
                        if (context.startLogin()) {
                            val intent = Intent(context, H5Activity::class.java)
                            intent.putExtra("type", item.type)
                            intent.putExtra("gid", item.gid)
                            context.startActivity(intent)
                        }
                    }
                }
            }
        }
    }
}